package entities;

import menu.Settings;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import util.Vector2D;

public class BulletExplosionEnemy extends Enemy {

	private boolean velocityInitialized;
	private int bullets;
	
	public BulletExplosionEnemy(int bullets) throws SlickException {
		super("res/enemies/enemy01.png");
		this.bullets = bullets;
		image = image.getScaledCopy(.6f);
		maxHealth = (int) (7 * Settings.getfactor());
		health = maxHealth;
		score = 170;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (!velocityInitialized) {
			if (level.getPlayer() != null && level.isActive(level.getPlayer())) {
				setVelocity(level.getPlayer().center().subtract(center())
						.getUnitVector().scale(150));
				velocityInitialized = true;
			}
		}
		if (health == 0) {
			explode(bullets);
		}
		Vector2D distance = center().subtract(level.getPlayer().center());
		if (distance.getLength() < 200) {
			explode(bullets);
		}
	}

	public void explode(int n) {
		Vector2D v = new Vector2D(1.0f, 1.0f);
		float dAlpha = 360.0f / n;
		for (int i = 0; i < n; i++) {
			try {
				Shot shot = new Shot("res/shot.png", this, 1, v.scale(200));
				shot.spawn(level, center());
			} catch (SlickException e) {
				e.printStackTrace();
			}
			v = v.rotate(dAlpha);
		}
		level.removeEntity(this);
	}

	@Override
	public String getName() {
		return "Bullet Explosion Enemy";
	}

	public void reset() {
		super.reset();
		velocityInitialized = false;
	}

}
